package com.jai.pms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.ProjectMapper;
import com.jai.pms.domain.Project;
import com.jai.pms.service.IProjectService;

/**
 * 项目管理Service业务层处理
 * 
 * @author v
 * @date 2023-10-18
 */
@Service
public class ProjectServiceImpl implements IProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目管理
     * 
     * @param id 项目管理主键
     * @return 项目管理
     */
    @Override
    public Project selectProjectById(Long id) {
        return projectMapper.selectProjectById(id);
    }

    /**
     * 查询项目管理列表
     * 
     * @param project 项目管理
     * @return 项目管理
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        return projectMapper.selectProjectList(project);
    }

    /**
     * 新增项目管理
     * 
     * @param project 项目管理
     * @return 结果
     */
    @Override
    public int insertProject(Project project) {
        preCheck(project, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        project.setCreateBy(loginUser.getUsername());
        project.setCreateTime(DateUtils.getNowDate());
        return projectMapper.insertProject(project);
    }

    /**
     * 修改项目管理
     * 
     * @param project 项目管理
     * @return 结果
     */
    @Override
    public int updateProject(Project project) {
        preCheck(project, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        project.setUpdateBy(loginUser.getUsername());
        project.setUpdateTime(DateUtils.getNowDate());
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目管理
     * 
     * @param ids 需要删除的项目管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectByIds(Long[] ids) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("ids", ids);
        params.put("updateBy", SecurityUtils.getLoginUser().getUsername());
        params.put("updateTime", DateUtils.getNowDate());
        return projectMapper.deleteProjectByIds(params);
    }

    /**
     * 删除项目管理信息
     * 
     * @param id 项目管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectById(Long id)
    {
        return projectMapper.deleteProjectById(id);
    }

    private void preCheck(Project project, boolean isAdd) {
        // 检查是否有重复信息，联合主键code和version
        Project query = new Project();
        query.setCode(project.getCode());
        List<Project> queryList = projectMapper.selectProjectList(query);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该项目已经存在");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), project.getId())) {
                throw new ServiceException("该项目已经存在");
            }
        }
    }
}
